package com.voxel.sense.infomsg.module.firm.details.average;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.AverageDataBean;
import com.voxel.sense.infomsg.data.bean.MultiItemBean;

import java.util.List;

import butterknife.BindView;

/**
 * Created by ChenYiYao on 2018/8/1.
 */
public class AverageRecycleAdapter extends RecyclerAdapter<MultiItemBean> {

    @Override
    protected int getItemViewType(int position, MultiItemBean bean) {
        //小于mElementCount个数的，则是表格数据，否则为图标
        return R.layout.item_recycler_history_data;
    }

    @Override
    protected ViewHolder<MultiItemBean> onCreateViewHolder(View root, int viewType) {

        return new NorLineViewHolder(root);
    }

    static class NorLineViewHolder extends ViewHolder<MultiItemBean> {
        @BindView(R.id.tv_date_day)
        TextView tv_date_day;//日期月日
        @BindView(R.id.tv_date_hours)
        TextView tv_date_hours; //十分
        @BindView(R.id.ll_element_list)
        LinearLayout ll_element_list; //元素item的父view

        private NorLineViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(MultiItemBean tableDataBean) {
            List<AverageDataBean> data = (List<AverageDataBean>) tableDataBean.getData();

            tv_date_hours.setVisibility(View.INVISIBLE);

            //添加元素
            for (int i = 0; i < data.size(); i++) {
                TextView textView = (TextView) ll_element_list.getChildAt(i);
                if (textView == null) {
                    Context context = ll_element_list.getContext();//动态添加
                    textView = (TextView) LayoutInflater.from(context).inflate(R.layout.layout_history_data_textview, null);
                    ll_element_list.addView(textView);
                }
                AverageDataBean dataBean = data.get(i);
                if (i == 0) tv_date_day.setText(dataBean.getTime());
                textView.setVisibility(View.VISIBLE);
                //设置参数内容
                String context = TextUtils.isEmpty(dataBean.getParamUnit()) ? (dataBean.getParamName() + " : " + dataBean.getValue()) :
                        (dataBean.getParamName() + " : " + dataBean.getValue() + "(" + dataBean.getParamUnit() + ")");
                textView.setText(context);
            }
            //子view多余真实元数，则隐藏多余的
            if (data.size() < ll_element_list.getChildCount()) {
                for (int i = ll_element_list.getChildCount() - 1; i >= data.size(); i--) {
                    ll_element_list.getChildAt(i).setVisibility(View.GONE);
                }
            }

        }
    }
}
